<?php 

class Controller_Campaign extends Controller_Abstract
{
	function actionShow()
	{	
		$this->_view['publishers'] = DataUtil::getOptionsForPublishers();			
		
		$this->_view['subs'] = DataUtil::getOptionsForSubs();
		
		$this->_view['offers'] = DataUtil::getOptionsForOffers();
		
		$cammodel = new Campaign();
		if(array_search('PUBLISHER', $this->_app->currentUserRoles())){
			$user = $this->_app->currentUser();
			$publiserId = $user['pub_id'];
		}
		if($this->_context->isGET()){
			$campaigns = $cammodel->getAllCampaigns($publiserId);
			if (isset($_SESSION['infos'])){
				$infos = $_SESSION['infos'];
				$this->_view['infos'] = $infos;
				unset($_SESSION['infos']);
			}
		}elseif ($this->_context->isPOST()){
			$name = $this->_context->name;
			$publisher = $this->_context->publisher;
			$sub = $this->_context->sub;
			$offer = $this->_context->offer;
			$campaigns = $cammodel->search($name, $publisher, $sub, $offer);
			$this->_view['name'] = $name;
			$this->_view['publisher'] = $publisher;
			$this->_view['sub'] = $sub;
			$this->_view['offer'] = $offer;
		}
		$this->_view['campaigns'] = $campaigns;
	}
	
	function actionAdd()
	{
		
		$this->_view['offers'] = DataUtil::getOptionsForOffersList();
		
		$this->_view['publishers'] = DataUtil::getOptionsForPublishers();
		
		
		if ($this->_context->isPOST())
		{
			$errors = array();
			$infos = array();
			$name = $this->_context->name;
			if (empty($name))
			{
				$errors[] = Util::getMessage(1005);
			}
			$publisher = $this->_context->publisher;
			if (empty($publisher))
			{
				return $this->_redirect(url('default/index'));
			}
			$sub = $this->_context->sub;
			$offer = $this->_context->offer;
			$pixel = $this->_context->pixel;
			$pixelType = $this->_context->pixel_type;
			if (count($errors) == 0)
			{
				$cammodel = new Campaign();
				$cammodel->add($name, $publisher, $sub, $offer, $pixel, $pixelType);
				$infos[] = Util::getMessage(4001);
				$this->_view['infos'] = $infos;
			}
			else
			{
				$this->_view['name'] = $name;
				$this->_view['errors'] = $errors;
			}
		}
	}
	function actionCreative()
	{
		$camid = $this->_context->cam_id;
		if (empty($camid))
		{
			return $this->_redirect(url('default/index'));
		}
		$cammodel = new Campaign();
		$campaign = $cammodel->getCampaignById($camid);
		if ($campaign == false)
		{
			return $this->_redirect(url('default/index'));
		}
		$this->_view['campaign'] = $campaign;
		$siteid = $campaign['site_id'];
		$cremodel = new Creative();
		$creatives = $cremodel->getCreativesBySite($siteid);
		$this->_view['creatives'] = $creatives;
		if ($this->_context->isPOST())
		{
			$cammodel->deleteCreatives($campaign['id']);
			foreach($creatives as $creative)
			{
				if (isset($_POST['cre'.$creative['id']]))
				{
					$cammodel->addCreative($campaign['id'], $creative['id']);
				}
			}
			$infos = array();
			$infos[] = Util::getMessage(4002);
			$this->_view['infos'] = $infos;
		}
		$selected = $cammodel->getSeletedCreativesByCampaignId($camid);
		$this->_view['selected'] = $selected;
	}
}
?>